package tw.zld369504.wol.activity;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import tw.zld369504.wol.action.DeviceListActivityController;
import tw.zld369504.wol.R;
import tw.zld369504.wol.base.BaseActivity;

public class DeviceListActivity extends BaseActivity {

    private FloatingActionButton btn_add_profile;
    private ListView listView;
    private ViewImpl impl;
    private DeviceListActivityController mController;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_device_list_activity);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mController=new DeviceListActivityController(this);
        mController.onCreate(null);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mController.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mController.loadProfileFromDb();
    }

    private void initView() {
        impl=new ViewImpl();
        btn_add_profile = (FloatingActionButton) findViewById(R.id.btn_add_profile);
        btn_add_profile.setOnClickListener(impl);
        listView=(ListView) findViewById(R.id.listView);
        listView.setOnItemClickListener(impl);
    }

    private class ViewImpl implements View.OnClickListener,AdapterView.OnItemClickListener{

        @Override
        public void onClick(View view) {
            if(view==btn_add_profile){
                //TODO jump to add profile activity
            }
        }

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            //TODO select one profile and return it to previous activity
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId()==android.R.id.home){
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);

    }
}
